<?php
// students_api.php
include 'config.php';

$database = new Database();
$db = $database->getConnection();

$method = $_SERVER['REQUEST_METHOD'];

switch($method) {
    case 'GET':
        // 获取学生列表
        if(isset($_GET['class'])) {
            $class = $_GET['class'];
            getStudents($db, $class);
        } else {
            http_response_code(400);
            echo json_encode(["error" => "缺少班级参数"]);
        }
        break;
        
    case 'POST':
        // 添加新学生
        addStudent($db);
        break;
        
    case 'PUT':
        // 更新学生信息
        updateStudent($db);
        break;
        
    case 'DELETE':
        // 删除学生
        deleteStudent($db);
        break;
        
    default:
        http_response_code(405);
        echo json_encode(["error" => "不支持的请求方法"]);
        break;
}

// 获取学生列表
function getStudents($db, $class) {
    try {
        $table_name = $class == 1 ? "数字媒体技术1班" : "数字媒体技术2班";
        $homepage_field = $class == 1 ? "作业主页" : "主页";
        
        $query = "SELECT 学号 as id, 姓名 as name, $homepage_field as homepage FROM `$table_name`";
        $stmt = $db->prepare($query);
        $stmt->execute();
        
        $students = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        echo json_encode([
            "success" => true,
            "data" => $students,
            "count" => count($students)
        ]);
    } catch(PDOException $exception) {
        http_response_code(500);
        echo json_encode(["error" => "获取学生数据失败: " . $exception->getMessage()]);
    }
}

// 添加学生
function addStudent($db) {
    try {
        $input = json_decode(file_get_contents("php://input"), true);
        
        if(!isset($input['class']) || !isset($input['id']) || !isset($input['name'])) {
            http_response_code(400);
            echo json_encode(["error" => "缺少必要参数"]);
            return;
        }
        
        $class = $input['class'];
        $table_name = $class == 1 ? "数字媒体技术1班" : "数字媒体技术2班";
        $homepage_field = $class == 1 ? "作业主页" : "主页";
        
        $query = "INSERT INTO `$table_name` (学号, 姓名, $homepage_field) VALUES (:id, :name, :homepage)";
        $stmt = $db->prepare($query);
        
        $stmt->bindParam(":id", $input['id']);
        $stmt->bindParam(":name", $input['name']);
        $stmt->bindParam(":homepage", $input['homepage']);
        
        if($stmt->execute()) {
            echo json_encode(["success" => true, "message" => "学生添加成功"]);
        } else {
            http_response_code(500);
            echo json_encode(["error" => "添加学生失败"]);
        }
    } catch(PDOException $exception) {
        http_response_code(500);
        echo json_encode(["error" => "添加学生失败: " . $exception->getMessage()]);
    }
}

// 更新学生信息
function updateStudent($db) {
    try {
        $input = json_decode(file_get_contents("php://input"), true);
        
        if(!isset($input['class']) || !isset($input['id']) || !isset($input['name'])) {
            http_response_code(400);
            echo json_encode(["error" => "缺少必要参数"]);
            return;
        }
        
        $class = $input['class'];
        $table_name = $class == 1 ? "数字媒体技术1班" : "数字媒体技术2班";
        $homepage_field = $class == 1 ? "作业主页" : "主页";
        
        $query = "UPDATE `$table_name` SET 姓名 = :name, $homepage_field = :homepage WHERE 学号 = :id";
        $stmt = $db->prepare($query);
        
        $stmt->bindParam(":id", $input['id']);
        $stmt->bindParam(":name", $input['name']);
        $stmt->bindParam(":homepage", $input['homepage']);
        
        if($stmt->execute()) {
            if($stmt->rowCount() > 0) {
                echo json_encode(["success" => true, "message" => "学生信息更新成功"]);
            } else {
                http_response_code(404);
                echo json_encode(["error" => "学生不存在"]);
            }
        } else {
            http_response_code(500);
            echo json_encode(["error" => "更新学生信息失败"]);
        }
    } catch(PDOException $exception) {
        http_response_code(500);
        echo json_encode(["error" => "更新学生信息失败: " . $exception->getMessage()]);
    }
}

// 删除学生
function deleteStudent($db) {
    try {
        $input = json_decode(file_get_contents("php://input"), true);
        
        if(!isset($input['class']) || !isset($input['id'])) {
            http_response_code(400);
            echo json_encode(["error" => "缺少必要参数"]);
            return;
        }
        
        $class = $input['class'];
        $table_name = $class == 1 ? "数字媒体技术1班" : "数字媒体技术2班";
        
        $query = "DELETE FROM `$table_name` WHERE 学号 = :id";
        $stmt = $db->prepare($query);
        $stmt->bindParam(":id", $input['id']);
        
        if($stmt->execute()) {
            if($stmt->rowCount() > 0) {
                echo json_encode(["success" => true, "message" => "学生删除成功"]);
            } else {
                http_response_code(404);
                echo json_encode(["error" => "学生不存在"]);
            }
        } else {
            http_response_code(500);
            echo json_encode(["error" => "删除学生失败"]);
        }
    } catch(PDOException $exception) {
        http_response_code(500);
        echo json_encode(["error" => "删除学生失败: " . $exception->getMessage()]);
    }
}
?>